package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.example.demo.service.CreativeassistanceFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/12/18 20:19
 */
@Controller
public class CreativeassistanceController {

    @Autowired
    private CreativeassistanceFeignService creativeassistanceFeignService;

    @RequestMapping("/creativeassistance")
    public String topage(HttpServletRequest httpServletRequest, Model model) {

//    public String topage(HttpServletRequest httpServletRequest,@RequestParam(value = "list",required = false) List<String> list, @RequestParam(value = "s",required = false) String s, Model model) {
//        String s1 = (String) httpServletRequest.getAttribute("s");
//        System.out.println(s1);
//        System.out.println(s);
//        if (s != null) {
//            list.add(s);
//        }
//        model.addAttribute("list", list);
        String s = (String) httpServletRequest.getAttribute("s");

        List<String> list;
        String str = (String) httpServletRequest.getAttribute("list");
        if (str!=null)
        {
            str = StringUtils.stripStart(str, "[");//去掉前[
            str = StringUtils.stripEnd(str, "]");//去掉后]
            List<String> d = Arrays.asList(StringUtils.split(str,","));
          list   = new ArrayList<String>(d);
        }else {
            list=new ArrayList<String>();
        }


        if (s != null) {



                list.add(s);


        }

        model.addAttribute("list", list);
        return "creativeassistance";
    }

    @PostMapping("/automaticallyGenerateFemale")
    public String automaticallyGenerateFemale(HttpServletRequest httpServletRequest, @RequestParam("type") String type) {
        String s = creativeassistanceFeignService.getFemaleAppearance(type);
        httpServletRequest.setAttribute("s", s);

        String list = httpServletRequest.getParameter("list");
        httpServletRequest.setAttribute("list",list);

        return "forward:/creativeassistance";

    }

    @PostMapping("/automaticallyGenerateMale")
    public String automaticallyGenerateMale(HttpServletRequest httpServletRequest, HttpServletResponse response, @RequestParam("type") String type) {
        String s = creativeassistanceFeignService.getMaleAppearance(type);
        httpServletRequest.setAttribute("s", s);
        String list = httpServletRequest.getParameter("list");



        httpServletRequest.setAttribute("list",list);
          return "forward:/creativeassistance";
    }

}
